package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ser.Serializers;
import edu.bit.water.entity.MetaTI;
import edu.bit.water.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import edu.bit.water.entity.MetaSensor;
import edu.bit.water.service.MetaSensorAssociationService;
import edu.bit.water.service.MetaSensorService;


@Controller
@RequestMapping("/meta_sensor")
public class MetaSensorController extends BaseController {

	@Autowired
	private MetaSensorService metaSensorService;
	@Autowired
	private MetaSensorAssociationService metaSensorAssociationService;

	/**
	 ** 查询元数据表
	 **
	 * @return Map<String, Object> 如果查询成功则success为true,并返回metaSensorList
	 *         如果失败则返回false,errMsg中为结果
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Map<String, Object> queryMataSensor(@RequestParam("techProcessId") String techProcessId) {
		Map<String, Object> modelMap = new HashMap<>();
		List<MetaSensor> metaSensorList = metaSensorService.selectData(techProcessId);
		modelMap.put("status", "success");
		modelMap.put("rows", metaSensorList);
		return modelMap;
	}

	/**
	 *
	 * @param metaSensor 元数据设备表实体类
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insert(MetaSensor metaSensor) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		metaSensor.setCompany_id(userDao.getCompanyIdByUserId(user.getId()));
		return metaSensorService.insertMetaSensor(metaSensor);
	}

	/**
	 *
	 *
	 *  元数据设备表实体类
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(@RequestBody List<String> idList) {
		int flag = metaSensorService.deleteMetaSensor(idList);
		Map<String, Object> modelMap = new HashMap<>();
		if(flag == 1) {
			modelMap.put("status", "success");
		} else {
			modelMap.put("status", "failed");
		}
		return modelMap;
	}
	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> updateMetaSensor(MetaSensor metasonsor) {
		Map<String, Object> result = metaSensorService.updateMetaSensor(metasonsor);
		return result;
	}
}

